package com.example.library_common.album.utils;

import android.content.Context;
import android.os.Build;

import androidx.annotation.RawRes;
import androidx.annotation.RequiresApi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class Utils {


    public static String getJsonFromRaw(Context context, @RawRes int rawId) {
        //获取文件中的内容
        InputStream inputStream = context.getResources().openRawResource(rawId);
        //将文件中的字节转换为字符
        InputStreamReader isReader = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            isReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
        }
        //使用bufferReader去读取字符
        BufferedReader reader = new BufferedReader(isReader);
        StringBuilder out = new StringBuilder();
        String line;
        try {
            while ((line = reader.readLine()) != null) {

                out.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
                isReader.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return out.toString();
    }

}
